﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Styx;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.CommonBot.Inventory;

using BrodieMobList;

namespace BrodiesPlugin
{
    class Tabards
    {
        public static void TabardCheck()
        {
            if (!StyxWoW.Me.IsInInstance)
                return;

            // Complete Factional Listing for Tabard Reps
            // Horde
            uint OrgFaction = 76;
            uint TBFaction = 81;
            uint BilgewaterFaction = 1133;
            uint DarkSFaction = 530;
            uint HuojinFaction = 1352;
            uint UndercityFaction = 68;
            uint SilvermoonFaction = 911;
            uint DragonmawFaction = 1172;
            // Alliance
            uint SWFaction = 72;
            uint IFFaction = 47;
            uint GnomereganFaction = 54;
            uint TushuiFaction = 1353;
            uint DarnFaction = 69;
            uint ExodarFaction = 930;
            uint GilneasFaction = 1134;
            uint WildhammerFaction = 1174;
            // Neutral
            uint RamkahenFaction = 1173;
            uint EarthenFaction = 1135;
            uint HyjalFaction = 1158;
            uint TherazaneFaction = 1171;

            if (StyxWoW.Me.IsHorde)
            {
                if (StyxWoW.Me.MapId == 0 || StyxWoW.Me.MapId == 1)
                    if (CanWeEquip(BMobList.OrgrimmarTabard, OrgFaction, 45581))
                        EquipTabard(BMobList.OrgrimmarTabard);
                    else if (CanWeEquip(BMobList.DarkspearTabard, DarkSFaction, 45582))
                        EquipTabard(BMobList.DarkspearTabard);
                    else if (CanWeEquip(BMobList.ThunderBluffTabard, TBFaction, 45584))
                        EquipTabard(BMobList.ThunderBluffTabard);
                    else if (CanWeEquip(BMobList.UndercityTabard, UndercityFaction, 45583))
                        EquipTabard(BMobList.UndercityTabard);
                    else if (CanWeEquip(BMobList.SilvermoonTabard, SilvermoonFaction, 45585))
                        EquipTabard(BMobList.SilvermoonTabard);
                    else if (CanWeEquip(BMobList.BilgewaterTabard, BilgewaterFaction, 64484))
                        EquipTabard(BMobList.BilgewaterTabard);
                    else if (CanWeEquip(BMobList.HuojinTabard, HuojinFaction, 83080))
                        EquipTabard(BMobList.HuojinTabard);
                    else
                        return;
            }
            if (StyxWoW.Me.IsAlliance)
            {
                if (StyxWoW.Me.MapId == 0 || StyxWoW.Me.MapId == 1)
                    if (CanWeEquip(BMobList.StormwindTabard, SWFaction, 45574))
                        EquipTabard(BMobList.StormwindTabard);
                    else if (CanWeEquip(BMobList.IronforgeTabard, IFFaction, 45577))
                        EquipTabard(BMobList.IronforgeTabard);
                    else if (CanWeEquip(BMobList.DarnassusTabard, DarnFaction, 45579))
                        EquipTabard(BMobList.DarnassusTabard);
                    else if (CanWeEquip(BMobList.GnomereganTabard, GnomereganFaction, 45578))
                        EquipTabard(BMobList.GnomereganTabard);
                    else if (CanWeEquip(BMobList.ExodarTabard, ExodarFaction, 45580))
                        EquipTabard(BMobList.ExodarTabard);
                    else if (CanWeEquip(BMobList.GilneasTabard, GilneasFaction, 64882))
                        EquipTabard(BMobList.GilneasTabard);
                    else if (CanWeEquip(BMobList.TushuiTabard, TushuiFaction, 83079))
                        EquipTabard(BMobList.TushuiTabard);
                    else
                        return;
            }
        }

        public static bool CanWeEquip(WoWItem tabard, uint rep, int entry)
        {
            if (StyxWoW.Me.GetReputationLevelWith(rep) >= WoWUnitReaction.Exalted)
                return false;
            if (tabard != null || StyxWoW.Me.Inventory.Equipped.Tabard.Entry == entry)
                return true;
            return false;
        }

        public static void EquipTabard(WoWItem tabard)
        {
            if (tabard != null)
                Lua.DoString("EquipItemByName (\"" + tabard.Entry + "\")");
        }

        public static void SetUpTabardScreen(string name, uint rep, WoWItem tabard, int entry, TextBox tb, Label label, PictureBox pb)
        {
            tb.Text = Convert.ToString(StyxWoW.Me.GetReputationLevelWith(rep));
            label.Text = name;
            if (StyxWoW.Me.GetReputationLevelWith(rep) >= WoWUnitReaction.Exalted)
                pb.BackgroundImage = global::BrodiesPlugin.Properties.Resources.greenlight;
            else if (tabard != null || (StyxWoW.Me.Inventory.Equipped.Tabard != null && StyxWoW.Me.Inventory.Equipped.Tabard.Entry == entry))
                pb.BackgroundImage = global::BrodiesPlugin.Properties.Resources.yellowlight;
            else
                pb.BackgroundImage = global::BrodiesPlugin.Properties.Resources.redlight;
        }
    }
}